﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.paypal.sdk.services;
using com.paypal.soap.api;
using com.paypal.sdk.profiles;
using System.Data;

namespace FYPDemo
{
    public partial class MakePayment : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string OrderID = Session["orderID"].ToString();
                getOrder(OrderID);
            }
        }

        protected void getOrder(string orderID)
        {
            DataTable dt = Payment.listOrder(orderID);
            lblOrderID.Text = dt.Rows[0][1].ToString();
            lblDate.Text = dt.Rows[0][2].ToString();
            lblAmount.Text = dt.Rows[0][3].ToString();
            lblName.Text = dt.Rows[0][6].ToString();
        } 

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            int result = 0;
            double totalPrice = Convert.ToDouble(lblAmount.Text);
            ValidateCardNumber.CCChecker ccc = new ValidateCardNumber.CCChecker();
            string ccCheck = "true"; // ccc.ValidateCardNumber(ddlType.SelectedValue, txtCCN.Text);
            string subject = "Successful pay the bill";
            string body = "Dear Mr/Mrs " + lblName.Text + "<br /><br />You have paid the order " +lblOrderID.Text+ " with amount " +lblAmount.Text;
            DataTable dt = Member.showMemberDetails(lblName.Text);
            string email = "";

            if (dt.Rows.Count > 0)
                email = dt.Rows[0][5].ToString();

            if (ccCheck == "true")
            {                
                string orderID = Session["orderID"].ToString();
                
                string name = Session["username"].ToString();
                //string shippingID = "";
                string payment = sandBox();
                if (payment == "Success")
                {
                    result = Payment.makePayment(orderID);

                    if (result > 0)
                    {
                        Payment.CalculateRewardPoint(name, totalPrice);
                        MessageBox.Show("Successfully pay the order " + orderID);
                        SendMail.SendEmail(email, subject, body);
                    }
                }
                else
                {
                        MessageBox.Show("Payment unsuccessfully");
                }
            }
            else
            {
                MessageBox.Show("Invalid credit card number.");
            }
        }

        private string sandBox()
        {
            CallerServices caller = new CallerServices();

            IAPIProfile profile = ProfileFactory.createSignatureAPIProfile();

            profile.APIUsername = "univS_1328426803_biz_api1.gmail.com";
            profile.APIPassword = "1328426839";
            profile.APISignature = "Akr-ZVLKtAjVj-fstXdy.fr3D9HyAOzyzx1.QjYyLj--w.cNr0dTPHG1";
            profile.Environment = "sandbox";
            caller.APIProfile = profile;

            PaymentActionCodeType paymentAction = PaymentActionCodeType.Sale;

            //Create the request object.
            DoDirectPaymentRequestType pp_Request = new DoDirectPaymentRequestType();
            pp_Request.Version = "51.0";

            //Add request-specific fields to the request.
            //Create the request details object.
            pp_Request.DoDirectPaymentRequestDetails = new DoDirectPaymentRequestDetailsType();

            pp_Request.DoDirectPaymentRequestDetails.IPAddress = "192.168.1.1";
            pp_Request.DoDirectPaymentRequestDetails.MerchantSessionId = "1X911810264059026";
            pp_Request.DoDirectPaymentRequestDetails.PaymentAction = paymentAction;

            pp_Request.DoDirectPaymentRequestDetails.CreditCard = new CreditCardDetailsType();

            string creditCardNumber = txtCCN.Text;//"4632908900738149";

            string creditCardType = ddlType.SelectedValue;// "Visa";
            int expMonth = int.Parse(ddlMonth.SelectedValue);// 11;
            int expYear = int.Parse(txtYear.Text);//2016
            string CVV2 = txtCVV.Text;//"111";

            //string buyerAddress1 = txtAdd1.Text;//"1 Main St";
            //string buyerAddress2 = txtAdd2.Text;//"";
            //string buyerCity = txtCity.Text;// "San Jose";
            //string buyerState = ddlState.SelectedValue;//"CA";
            //string buyerZipCode = txtPostcode.Text;// "95131";

            string buyerFirstName = lblName.Text;
            //string buyerLastName = "";
            string paymentAmount = lblAmount.Text;//"100";

            pp_Request.DoDirectPaymentRequestDetails.CreditCard.CreditCardNumber = creditCardNumber;
            switch (creditCardType)
            {
                case "Visa":
                    pp_Request.DoDirectPaymentRequestDetails.CreditCard.CreditCardType = CreditCardTypeType.Visa;
                    break;
                case "MasterCard":
                    pp_Request.DoDirectPaymentRequestDetails.CreditCard.CreditCardType = CreditCardTypeType.MasterCard;
                    break;
                case "Discover":
                    pp_Request.DoDirectPaymentRequestDetails.CreditCard.CreditCardType = CreditCardTypeType.Discover;
                    break;
                case "Amex":
                    pp_Request.DoDirectPaymentRequestDetails.CreditCard.CreditCardType = CreditCardTypeType.Amex;
                    break;
            }
            pp_Request.DoDirectPaymentRequestDetails.CreditCard.CVV2 = CVV2;
            pp_Request.DoDirectPaymentRequestDetails.CreditCard.ExpMonth = expMonth;
            pp_Request.DoDirectPaymentRequestDetails.CreditCard.ExpYear = expYear;
            pp_Request.DoDirectPaymentRequestDetails.CreditCard.ExpMonthSpecified = true;
            pp_Request.DoDirectPaymentRequestDetails.CreditCard.ExpYearSpecified = true;
            pp_Request.DoDirectPaymentRequestDetails.CreditCard.CardOwner = new PayerInfoType();
            pp_Request.DoDirectPaymentRequestDetails.CreditCard.CardOwner.Payer = "";
            pp_Request.DoDirectPaymentRequestDetails.CreditCard.CardOwner.PayerID = "";
            pp_Request.DoDirectPaymentRequestDetails.CreditCard.CardOwner.PayerStatus = PayPalUserStatusCodeType.unverified;
            pp_Request.DoDirectPaymentRequestDetails.CreditCard.CardOwner.PayerCountry = CountryCodeType.US;

            //pp_Request.DoDirectPaymentRequestDetails.CreditCard.CardOwner.Address = new AddressType();
            //pp_Request.DoDirectPaymentRequestDetails.CreditCard.CardOwner.Address.Street1 = buyerAddress1;
            //pp_Request.DoDirectPaymentRequestDetails.CreditCard.CardOwner.Address.Street2 = buyerAddress2;
            //pp_Request.DoDirectPaymentRequestDetails.CreditCard.CardOwner.Address.CityName = buyerCity;
            //pp_Request.DoDirectPaymentRequestDetails.CreditCard.CardOwner.Address.StateOrProvince = buyerState;
            //pp_Request.DoDirectPaymentRequestDetails.CreditCard.CardOwner.Address.PostalCode = buyerZipCode;
            //pp_Request.DoDirectPaymentRequestDetails.CreditCard.CardOwner.Address.CountryName = "USA";
            //pp_Request.DoDirectPaymentRequestDetails.CreditCard.CardOwner.Address.Country = CountryCodeType.US;
            //pp_Request.DoDirectPaymentRequestDetails.CreditCard.CardOwner.Address.CountrySpecified = true;

            pp_Request.DoDirectPaymentRequestDetails.CreditCard.CardOwner.PayerName = new PersonNameType();
            pp_Request.DoDirectPaymentRequestDetails.CreditCard.CardOwner.PayerName.FirstName = buyerFirstName;
            //pp_Request.DoDirectPaymentRequestDetails.CreditCard.CardOwner.PayerName.LastName = buyerLastName;
            pp_Request.DoDirectPaymentRequestDetails.PaymentDetails = new PaymentDetailsType();
            pp_Request.DoDirectPaymentRequestDetails.PaymentDetails.OrderTotal = new BasicAmountType();
            //NOTE: The only currency supported by the Direct Payment API at this time is US dollars (USD).

            pp_Request.DoDirectPaymentRequestDetails.PaymentDetails.OrderTotal.currencyID = CurrencyCodeType.USD;
            pp_Request.DoDirectPaymentRequestDetails.PaymentDetails.OrderTotal.Value = paymentAmount;

            //Execute the API operation and obtain the response.
            DoDirectPaymentResponseType pp_response = new DoDirectPaymentResponseType();
            pp_response = (DoDirectPaymentResponseType)caller.Call("DoDirectPayment", pp_Request);

            return pp_response.Ack.ToString();            
        }

        protected void btnPay_Click(object sender, EventArgs e)
        {
            int result = 0;
            double totalPrice = Convert.ToDouble(lblAmount.Text);
            ValidateCardNumber.CCChecker ccc = new ValidateCardNumber.CCChecker();
            string ccCheck = "true"; // ccc.ValidateCardNumber(ddlType.SelectedValue, txtCCN.Text);
            string subject = "Successful pay the bill";
            string body = "Dear Mr/Mrs " + lblName.Text + "<br /><br />You have paid the order " + lblOrderID.Text + " with amount " + lblAmount.Text;
            DataTable dt = Member.showMemberDetails(lblName.Text);
            string email = "";

            if (dt.Rows.Count > 0)
                email = dt.Rows[0][5].ToString();

            if (ccCheck == "true")
            {
                string orderID = Session["orderID"].ToString();

                string name = Session["username"].ToString();
                //string shippingID = "";
                string payment = sandBox();
                if (payment == "Success")
                {
                    result = Payment.makePayment(orderID);

                    if (result > 0)
                    {
                        Payment.CalculateRewardPoint(name, totalPrice);
                        MessageBox.Show("Successfully pay the order " + orderID);
                        SendMail.SendEmail(email, subject, body);
                    }
                }
                else
                {
                    MessageBox.Show("Payment unsuccessfully");
                }
            }
            else
            {
                MessageBox.Show("Invalid credit card number.");
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("OrdersSummary.aspx");
        }

    }
}